package com.mr.example1.index;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

/*
 * 1.输入数据：
 * 		atguigu pingping
 *
 * 2.输出数据：
 * 		atguigu-a.txt,1
 * 		pingping-a.txt,1
 */

public class Example1Mapper1 extends Mapper<LongWritable, Text, Text, LongWritable>{

	private String fileName;
	private Text outKey;

	@Override
	protected void setup(Mapper<LongWritable, Text, Text, LongWritable>.Context context)
			throws IOException, InterruptedException {

		// 获取切片
		FileSplit fileSplit = (FileSplit) context.getInputSplit();

		// 获取文件名
		fileName = fileSplit.getPath().getName();

	}

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
			throws IOException, InterruptedException {

		String[] words = value.toString().split(" ");

		for (String word : words) {

			// 在单词后面拼接文件名
			outKey = new Text(word + "-" + fileName);

		}

		context.write(outKey, new LongWritable(1));

	}

}
